/* eslint-disable @typescript-eslint/no-explicit-any */
/**
 * 当前格式化日期
 * 格式对应：y-年、M-月、d-天、h-时、m-分、s-秒、S-毫秒、q-季
 *
 * @export
 * @param { String } fmt
 * @return { String }
 *
 */
export const currentFormatTime = (fmt: string): string => {
  function dateFormat (this: any, format: string) {
    let result = format
    const timer:Record<string, any> = {
      'M+': this.getMonth() + 1, // month
      'd+': this.getDate(), // day
      'h+': this.getHours(), // hour
      'm+': this.getMinutes(), // minute
      's+': this.getSeconds(), // second
      'q+': Math.floor((this.getMonth() + 3) / 3), // quarter
      S: this.getMilliseconds() // millisecond
    }
    if (/(y+)/.test(result)) {
      result = result.replace(RegExp.$1, (`${this.getFullYear()}`).substr(4 - RegExp.$1.length))
    }
    Object.keys(timer).forEach((k:string) => {
      if (new RegExp(`(${k})`).test(result)) {
        result = result.replace(RegExp.$1, RegExp.$1.length === 1 ? timer[k] : (`00${timer[k]}`).substr((`${timer[k]}`).length))
      }
    })
    return result
  }
  return dateFormat.bind(new Date(), fmt)()
}
